package com.mcxx.modules.system.controller;

import com.mcxx.common.exception.BizException;
import com.mcxx.modules.filemanage.file.service.FileService;
import com.mcxx.modules.system.service.SalvationTaskExportService;
import com.mcxx.modules.system.entity.TaskExportEntity;
import com.mcxx.modules.system.param.StatisticsExportParam;
import com.mcxx.service.DefaultRestService;
import com.mcxx.util.ZipInfoUtil;
import com.yinhai.ta404.core.restservice.annotation.RestService;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.validate.annotation.V;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.UrlResource;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.io.File;


@Validated
@Api("系统管理/下载")
@RestService({"system/taskExport/v2"})
public class SalvationTaskExportController extends DefaultRestService {

    private final static Logger log = LoggerFactory.getLogger(SalvationTaskExportController.class);

    @Autowired
    private SalvationTaskExportService salvationTaskExportService;
    @Autowired
    private FileService fileService;


    @PostMapping("findList")
    @ApiOperation(value="下载列表页", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,notes="下载列表页")
    public void findList(PageParam pageParam){
        log.info("下载列表页查询开始，参数是：{}", pageParam);
        Page page = null;
        page = salvationTaskExportService.findPage(pageParam,getCurUserAccount());
        this.setPageBean(page);
        log.info("下载列表页查询结束，结果是：{}", page);
    }

    @RequestMapping(value = "downloadURL",method = RequestMethod.POST)
    @ApiOperation(value="下载文件",notes="下载文件")
    public void downloadURL(@RequestBody StatisticsExportParam param, HttpServletResponse response) {
        UrlResource resource = null;
        File file = null;
        try {
            TaskExportEntity entity = salvationTaskExportService.getById(param.getId());
            if(null != entity){
                byte[] bytes = fileService.download(entity.getFileUrl());
                if(null != bytes){
                    ZipInfoUtil.exportZipByte(response, entity.getTaskName(), bytes, this.getCurUserId());
                    salvationTaskExportService.update(param.getId());
                }
            }
        }catch (Exception e) {
            log.error(e.getMessage());
            throw new BizException("-1", e.getMessage());
        }
    }
    @RequestMapping(value = "deleteExport",method = RequestMethod.POST)
    @ApiOperation(value="删除下载列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,notes="删除下载列表")
    public void deleteExport(@V("notnull") String id){
        int rst = salvationTaskExportService.delete(id);
        setData("data", rst);
    }
}
